package com.luobd.bpm.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.luobd.bpm.business.process.input.ProcessTaskPageInput;
import com.luobd.bpm.business.process.input.TaskCommandInput;
import com.luobd.bpm.business.process.service.IProcessTaskService;
import com.luobd.bpm.common.*;
import com.luobd.bpm.dto.ProcessTaskDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/api/bpmn/task")
@Api(tags = "流程任务-自定义")
public class ProcessTaskController {

    @Autowired
    private IProcessTaskService processTaskService;



    @GetMapping(value = "/start")
    @ApiOperation(value = "发起流程")
    public ResponseData<Boolean> start(@RequestParam("id")String id) {
        return processTaskService.start(id);
    }



    @PostMapping(value = "/page")
    @ApiOperation(value = "分页查询流程任务")
    public ResponsePageData<ProcessTaskDTO> page(@RequestBody ProcessTaskPageInput input) {
        return processTaskService.page(input);
    }


    @PostMapping(value = "/pageMyTask")
    @ApiOperation(value = "分页查询流程任务-我的待办")
    public ResponsePageData<ProcessTaskDTO> pageMyTask(@RequestBody ProcessTaskPageInput input) {
        CurrentUserInfo userInfo = CurrentRequestHolder.get();
        input.setAssignee(String.valueOf(userInfo.getUserInfoId()));
        List<Role> roles = userInfo.getRoles();
        if(CollectionUtil.isNotEmpty(roles)) {
            List<String> list = roles.stream().map(v -> String.valueOf(v.getId())).collect(Collectors.toList());
            input.setGroups(list);
        }
        return processTaskService.page(input);
    }



    @PostMapping(value = "/resolve")
    @ApiOperation(value = "处理流程任务")
    public ResponseData<Boolean> resolve(@RequestBody @Valid TaskCommandInput input) {
        return processTaskService.resolveTask(input);
    }



    @GetMapping(value = "/recallByProcessInstance")
    @ApiOperation(value = "根据流程实例撤回任务（撤回到第一个任务）")
    public ResponseData<Boolean> recallByProcessInstance(@RequestParam("id")String id) {
        return processTaskService.start(id);
    }
}
